package com.tanhua.dubbo.api;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.CommentLike;
import com.tanhua.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@DubboService
public class CommentLikeApiImpl implements CommentLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 判断是否已点赞
     * @param commentId
     * @param userId
     * @return
     */
    @Override
    public Boolean hasLike(String commentId, Long userId) {
        Criteria criteria = Criteria.where("commentId").is(commentId)
                .and("userId").is(userId);
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query, CommentLike.class); //判断数据是否存在
    }

    @Override
    public void save(CommentLike commentLike) {
        //3、保存到数据库
        mongoTemplate.save(commentLike);
        //4、更新动态表中的对应字段
        Query query = Query.query(Criteria.where("id").is(commentLike.getCommentId()));
        Update update = new Update();
        update.inc("likeCount",1);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Comment modify = mongoTemplate.findAndModify(query, update, options, Comment.class);
    }

    /**
     * 视频评论删除点赞数据
     * @param commentLike
     */
    @Override
    public void delete(CommentLike commentLike) {
        Criteria criteria = Criteria.where("userId").is(commentLike.getUserId())
                .and("commentId").is(commentLike.getCommentId());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,CommentLike.class);

        Query query1 = Query.query(Criteria.where("id").is(commentLike.getCommentId()));
        Update update = new Update();
        update.inc("likeCount",-1);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Comment modify = mongoTemplate.findAndModify(query1, update, options, Comment.class);
    }

    @Override
    public Integer saveMon(CommentLike commentLike) {
        //3、保存到数据库
        mongoTemplate.save(commentLike);
        //4、更新动态表中的对应字段
        Query query = Query.query(Criteria.where("id").is(commentLike.getCommentId()));
        Update update = new Update();
        update.inc("likeCount",1);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Comment modify = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return modify.getLikeCount();
    }

    @Override
    public Integer deleteMon(CommentLike commentLike) {
        Criteria criteria = Criteria.where("userId").is(commentLike.getUserId())
                .and("commentId").is(commentLike.getCommentId());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,CommentLike.class);

        Query query1 = Query.query(Criteria.where("id").is(commentLike.getCommentId()));
        Update update = new Update();
        update.inc("likeCount",-1);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true) ;//获取更新后的最新数据
        Comment modify = mongoTemplate.findAndModify(query1, update, options, Comment.class);
        return modify.getLikeCount();
    }
}
